In this tutorial we’re going to walk you through the steps of configuring your stack as a private remote git repository. After this tutorial, you’ll be able to push and pull changes from and to your local machine. Perhaps the mainstream Git hosting solutions aren’t quite meeting your needs or you require increased security and privacy. Thankfully, its fairly simple to get a basic git server set up. Let’s get started.
If you don’t already have git installed on your Stack, you can install it by running
sudo apt-get install git
or
sudo yum install git
depending on whether you’re running Ubuntu or CentOS. After you’ve installed git, we need to make a user named ‘git’. Run the following commands on your stack.
adduser git
then give the ‘git’ user a password by running the following:
passwd git
Now we can switch to the new user by running ‘su – git’. If you’re not already changed into the home directory for this user, typing ‘cd ~’ will do the trick.
Now we need to configure SSH authentication so you only allow trusted people to use access your remote repository. We’re going to make a folder .ssh in the ‘git’ user’s home directory that will hold the SSH public keys of all the computers we wish to be able to push to this remote git repository.
mkdir -p .ssh
Once you’ve created this directory, we’re going to create a file inside this folder named ‘authorized_keys’. Let’s go ahead and create this file now.
touch ~/.ssh/authorized_keys
We want to be able to access our git server from a number of machines, whether from your mother’s desktop to your co-founder’s MacBook. On each machine that you wish to be able to push/pull changes from/to, you’ll have to run the following commands.
cat ~/.ssh/id_rsa.pub
If you get the error message “No such file or directory” then you haven’t yet generated a SSH key pair. We can guide you through the steps to generate your SSH keys without much background information as to how SSH public-key authentication works behinds the scene. We recommend you check out our detailed tutorial on SSH public-key authentication. On the machine from which you want to access your private git repository, run the following command:
ssh-keygen -C "[email protected]"
Replacing [email protected] appropriately. You’ll be asked to provide a file in which you wish to store your ssh keys. If you haven’t already generated ssh keys, the default location should be fine. After that, you’ll also be prompted to enter a passphrase. Although optional, it’s recommended to set a passphrase for your SSH keys. Your private ssh key is stored on your computer in plaintext. Thus, without a passphrase anyone who gains access to your private key could push and pull changes to your remote git repository.
Here’s what the output of the whole process should look like
[user@localhost ~]$ ssh-keygen -C "[email protected]" Generating public/private rsa key pair. Enter file in which to save the key (/home/user/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/user/.ssh/id_rsa. Your public key has been saved in /home/user/.ssh/id_rsa.pub. The key fingerprint is: 0c:2a:75:30:51:ff:72:55:f4:4d:a1:1c:d7:50:7a:32 [email protected] The key's randomart image is: +--[ RSA 2048]----+ | +o. o+==| | o . ..++o| | . o . .oE +| | . o o . . + | | . . S o | | . o | | | | | | | +-----------------+ Now if you run cat ~/.ssh/id_rsa.pub
on your local machine you should see a long string beginning with ‘ssh-rsa’ and ending with the email you provided to ssh-keygen. Copy this to your clipboard and hop onto your Stack once again. We’re now going to add this public key to the authorized_keys file you created earlier
nano ~/.ssh/authorized_keys
Now paste the key you copied previously here and save and close the file by pressing CTRL-X, then Y, followed by RETURN. You’re now set up to push/pull from and to a repository on this remote git server.
Let’s push some code to our remote repository to see if we’ve configured everything correctly. First, we’ll need to create the repository on our remote machine so git knows where to store and retrieve information from. cd into your home directory and type
git init --bare PROJECT_FOLDER_NAME.git
Remote git repositories usually follow a naming convention where the name of the repository is the name of the project directory, followed by the .git suffix. For example, if I had a project on my local machine named ‘nodejs_devel’ I would create a ‘nodejs_devel.git’ directory on my remote server. The ‘–bare’ flag just tells the ‘git init’ program to initialize a repository without any working directory. Thus, you wont be able to edit any code on your remote server but you will be able to securely push your git history to the repository.
Back on your local machine, cd into your project directory. If you haven’t already set up the directory as a local git repository, type ‘git init’ into your shell now. You’re now ready to set your server as the remote repository for your project. Enter the following commands
git remote add origin git@YOUR_SERVER_IP_ADDRESS:PROJECT_FOLDER_NAME.git
With this command, you’re telling git that you wish to create a new link to a remote repository located on your Stack. You’re going to call this link ‘origin’ if you want your remote server to be the main server to/from which you push and pull changes.
If you get an error saying ‘fatal: remote origin already exists’ then you’ve already created a link named ‘origin’ to some other remote git repository. You can either name your link something else and proceed, or remove the other link by running ‘git remote remove origin’ and reentering the ‘git remote add origin…’ command above.
Now that you’ve created the link between your local repository and your remote repository, you’re ready to push changes! Type in
git add .
git commit -m "Pushing changes to my very own remote git repository!"
git push origin master
You’re now set up to use your Stack as a private git repository. If you’re allowing multiple people to access this repository, it might be helpful to make directories in the home directory of the ‘git’ user on your remote server. For example, if Bob and Abe ask you if they can use your fancy new private git repository, you can create directories /home/git/bob and /home/git/abe for them to use. You’ll have to create new repositories for all the users you allow to use your private repository by using ‘git init –bare /home/git/USER/PROJECT_NAME.git’. But after you’ve made the repository, all they have to do is run the command
git remote add origin git@YOUR_SERVER_IP_ADDRESS:USER/PROJECT_NAME.git
replacing the appropriate placeholders.
Thanks for anchoring your Stack with us at Stack Harbor. Check out our community section for more tutorials on the various functionality you can leverage on your Stack.